import { EnumBetResult, EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { RecmInfo, RoundInfo } from "../ExecutePlan";
import { IMoneyStrategy } from "../IMoneyStrategy";

export interface IBossMoneyStrategyOption {
    /**
     * 
     */
    countToWin: number;
    baseMoney: number;
}
export class BossMoneyStrategy implements IMoneyStrategy {
    option: IBossMoneyStrategyOption;
    baseMoney: number = 100;
    levelBaseMoney: number = 100;
    countToWin: number = 0;
    prevMoney: number = 0;
    currentMoney: number = 0;
    limitMoney: number = 1300;
    winBack: boolean = false;
    constructor(name: string, option?: IBossMoneyStrategyOption | null, start?: boolean) {
        this.strategyName = name;
        if (option) {
            this.option = option;
        }
        else {
            this.option = {
                countToWin: 5,
                baseMoney: 100
            };
        }

        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        throw new Error("Method not implemented.");
    }
    resetRecm(): void {
    }
    resetStrategy(): void {
        this.countToWin = this.option.countToWin;
        this.baseMoney = this.option.baseMoney;
        this.levelBaseMoney = this.baseMoney;
        this.currentMoney = this.baseMoney;
        this.winBack = false;
        this.limitMoney = this.fibo(this.baseMoney, this.countToWin);
    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let recmV = roundInfo.userRecm;
        let cmoney = this.currentMoney;
        let win = recmInfo.summaryInfo.winMoney - recmInfo.summaryInfo.failMoney;
        if (recmInfo.summaryInfo.prevResult == EnumBetResult.Success) {
            this.currentMoney = this.prevMoney + cmoney;
            this.prevMoney = cmoney;
            if (recmInfo.summaryInfo.lwin > this.countToWin && !this.winBack) {
                this.winBack = true;
                this.currentMoney = this.baseMoney * 5;
                this.prevMoney = 0;
            }
        }
        else if (recmInfo.summaryInfo.prevResult == EnumBetResult.Fail) {
            if (this.winBack) {
                this.currentMoney = this.baseMoney;
                this.prevMoney = 0;
            }
            else if (-win > this.limitMoney) {
                let base = this.fibo(1,this.countToWin);
                this.levelBaseMoney = Math.ceil(-win / this.countToWin);
                this.currentMoney = this.levelBaseMoney;
                this.prevMoney = 0;
            }

        }
        else { }

        return [recmV, this.currentMoney];
    }

    strategyName: string;
    debugCompoenent: string = "";
    copyValue(valueObj: any): void {
        let obj = valueObj as BossMoneyStrategy;
        if (obj) {
            this.countToWin = obj.countToWin;
            this.baseMoney = obj.baseMoney;
            this.levelBaseMoney = obj.levelBaseMoney;
            this.currentMoney = obj.currentMoney;
            this.winBack = obj.winBack;;
        }

    }

    fibo(baseMoney: number, count: number): number {
        if (count == 0 || count == 1) return baseMoney;
        else return this.fibo(baseMoney, count - 1) + this.fibo(baseMoney, count - 2);
    }
}